package com.csi.controller.user;

import com.alibaba.fastjson2.JSON;
import com.csi.domain.AccountCredDetails;
import com.csi.domain.BindAccount;
import com.csi.pojo.Result;
import com.csi.service.UserService;
import com.csi.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/user/queryBindAccountNoCredListController")
public class QueryBindAccountNoCredListController extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("application/json;charset=utf-8");

        PrintWriter out = resp.getWriter();

        String userNumber = req.getParameter("userNumber");

        UserService userService = new UserServiceImpl();

		// 遍历所有绑定卡列表
        List<BindAccount> bindAccounts = userService.queryBindAccountList(userNumber);

		// 遍历所有信用卡列表
        List<AccountCredDetails> accountCredDetails = userService.queryCredAccountList(userNumber);

		// 绑定卡列表，不包含信用卡
        List<BindAccount> bindAccountsNoCred = new ArrayList<>();

        for (BindAccount bindAccount : bindAccounts) {
            String accountNumber = bindAccount.getAccountNumber();
            boolean isCred = false;

            for (AccountCredDetails accountCredDetail : accountCredDetails) {
                String accountCredNumber = accountCredDetail.getAccountNumber();
                if (accountNumber.equals(accountCredNumber)) {
                    isCred = true;
                    break;
                }
            }

            if (!isCred) {
                bindAccountsNoCred.add(bindAccount);
            }
        }

        Result<List<BindAccount>> result = new Result<>();
        result.setCode(200);
        result.setMsg("success");
        result.setDatas(bindAccountsNoCred);

        out.print(JSON.toJSONString(result));

        out.flush();
        out.close();
    }
}
